package com.lambkit.module.cms.web.controller;

import com.lambkit.module.cms.core.template.TemplateManager;
import com.lambkit.module.cms.rpc.model.CmsArticle;
import com.lambkit.module.cms.rpc.model.CmsCategory;
import com.lambkit.module.cms.rpc.model.CmsTopic;
import com.lambkit.web.controller.LambkitController;
import com.jfinal.plugin.activerecord.Page;
import com.lambkit.db.sql.column.Example;

/**
 * 专题首页控制器
 */
//@RequestMapping(value = "/topic")
public class TopicController extends LambkitController {

    //private static final Log LOG = Log.getLog(TopicController.class);

    //@RequestMapping(value = "/{topicId}-{pageId}-{categoryId}", method = RequestMethod.GET)
    public void index() {
    	String action = getPara(0);
    	if("page".equalsIgnoreCase(action)) {
    		int page = getParaToInt(1, 1);
            // 专题列表
            int rows = getParaToInt(2, 10);
            Example cmsTopicExample = CmsTopic.sql().example();
            Page<CmsTopic> topics = CmsTopic.service().paginate(page, rows, cmsTopicExample);
            setAttr("topics", topics);
            render(TemplateManager.me().getCurrentWebPath() + "/topic/list.html");
    	} else if("url".equalsIgnoreCase(action)) {
    		Long topicId = getParaToLong(1);
    		CmsTopic topic = CmsTopic.service().findById(topicId);
    		redirect(topic.getUrl());
    	} else {
    		Long topicId = getParaToLong(0);
            if(topicId!=null) {
            	setAttr("topic", CmsTopic.service().findById(topicId));
            	setAttr("article", CmsArticle.service().findFirst(
            			CmsArticle.sql().andTopicIdEqualTo(topicId).andTypeEqualTo(0).example()));
            	Long categoryId = getParaToLong(1);
                if(categoryId!=null) {
                	setAttr("category", CmsCategory.service().findById(categoryId));
                }
                Long fcid = getParaToLong(2);
                if(fcid!=null) {
                	setAttr("filecg", CmsCategory.service().findById(fcid));
                }
                render(TemplateManager.me().getCurrentWebPath() + "/topic/info.html");
            } else {
            	keepPara();
            	render(TemplateManager.me().getCurrentWebPath() + "/topic/index.html");
            }
    	}
    }
    
    /**
     * 详情
     */
    public void detail() {
    	Long topicId = getParaToLong(0);
    	setAttr("topic", CmsTopic.service().findById(topicId));
    	setAttr("article", CmsArticle.service().findFirst(
    			CmsArticle.sql().andTopicIdEqualTo(topicId).andTypeEqualTo(0).example()));
    	Long categoryId = getParaToLong(1);
        if(categoryId!=null) setAttr("category", CmsCategory.service().findById(categoryId));
        render(TemplateManager.me().getCurrentWebPath() + "/topic/detail.html");
    }
}